package com.ugoodtech.mdcc.core.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String formatDate(Date date,String rule) {
        if(null!=date){
            SimpleDateFormat dateFormat = new SimpleDateFormat(rule);
            return dateFormat.format(date);
        }else{
            return "";
        }
    }

    public static Date getNextDayCalender(int dayNumber) {
        Date day;
        Calendar cal = Calendar.getInstance();

        //n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
        int n = 1;
        cal.add(Calendar.DATE, n * 7);
        cal.set(Calendar.DAY_OF_WEEK, (dayNumber + 2) % 7);
        day = cal.getTime();
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(day));
        return day;
    }

    public static Date getNextWeekCalender(int weekNumber, int dayNumber) {
        Date day;
        Calendar cal = Calendar.getInstance();

        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.WEEK_OF_MONTH, weekNumber);
        cal.set(Calendar.DAY_OF_WEEK, (dayNumber + 2) % 7);
        day = cal.getTime();
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(day));
        return day;
    }

    public static Date getNextMonthCalender(int monthNumber, int weekNumber, int dayNumber) {
        Date day;
        Calendar cal = Calendar.getInstance();
        int monthDay = cal.get(Calendar.MONTH);
        if (0 == monthDay % 3) {
            cal.add(Calendar.MONTH, 3 + monthNumber);
        } else if (1 == monthDay % 3) {
            cal.add(Calendar.MONTH, 2 + monthNumber);
        } else if (2 == monthDay % 3) {
            cal.add(Calendar.MONTH, 1 + monthNumber);
        }
        cal.set(Calendar.WEEK_OF_MONTH, weekNumber + 1);
        cal.set(Calendar.DAY_OF_WEEK, (dayNumber + 2) % 7);
        day = cal.getTime();
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(day));
        return day;
    }

    //由出生日期获得年龄
    public static int getAge(Date birthDay)  {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            }else{
                age--;
            }
        }
        return age;
    }

    //由年龄获取最早日期
    public static Date getDateByAge(Integer age)  {
        if (null!=age) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR,-age);
            return cal.getTime();
        }else {
            return null;
        }
    }

}
